package com.sunjob.loginserver.service;

import cn.dev33.satoken.session.SaSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Wangzurong
 * @create 2022-08-07 1:14
 */
@Service
public class TokenServiceImpl implements TokenService {
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    @Override
    public List<SaSession> queryAllTokenSession() {
        Set<String> keys = redisTemplate.keys("satoken:login:token:*");
        ValueOperations<String, Object> stringObjectValueOperations = redisTemplate.opsForValue();
        List<Object> objects = stringObjectValueOperations.multiGet(keys);
        List<String> temp = new ArrayList<>();
        int i =0;
        for(String s:keys){
            if(!(""+objects.get(i++)).equals("-4")){
                // 未过期
                temp.add("satoken:login:token-session:"+s.substring(20,s.length()));
            }
        }
        return stringObjectValueOperations.multiGet(temp).stream().map(e->(SaSession)e).collect(Collectors.toList());
    }
}
